import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { SimpleTableColumn, SimpleTableComponent } from '@delon/abc';
import { NzMessageService, NzModalService, NzTabChangeEvent, UploadFile, NzNotificationService } from 'ng-zorro-antd';
import { CommService } from 'app/service/comm/comm.service';
import { Constant, ServiceResult } from '@core/constant';
import { EmitterService } from 'app/service/emit.service';
import { Router } from '@angular/router';
import { maService } from '../../../service/ma/ma.service';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';
import { Timeout } from '@shared/descriptor/timeout';
import {spListService} from "../../../service/spList/spList.service";
import {poListService} from "../../../service/poList/poList.service";
import {formatDate} from "@angular/common";




@Component({
  selector: 'poList-list',
  templateUrl: './poList-list.component.html',
  styleUrls: ['./poList-list.component.less']
})
export class poListComponent implements OnInit, OnDestroy {

  // region 公共字段

  visible = false;
  // pageSize
  ps = 10;
  // 搜索表单
  q: any = {
    lifnr:"",
    elikz:"I",
    name1: '',
    ebeln: '',
    matnr:'',
    idnlf:'',
    RETPO:''
  };

  loading = false;
  // 表格组件
  @ViewChild('st') st: SimpleTableComponent;
  // 表格数据url
  url = this.poListService.list();
  // region end

  // 搜索区域显示/隐藏标记
  // 显示图片
  previewVisible = false;
  // 大图地址
  previewImage = '';
  imgSuffix = '';

  years = [];
  userGroups = [];
  brands = [];
  seasons = [];
  checkedList = [];
  data = [];
  downurl='';
  uploadExcelUrl = this.spListService.uploadpc();

  elikzs=[
    { text: '未完成', value: 'I' },
    { text: '已完成', value: 'X' },

  ];

loaddata(wqtype){
  console.log(wqtype);
  if(wqtype=='X'){
    this.q.RETPO =wqtype;
    this.q.elikz ="";

  }else{
    this.q.RETPO ="";
    this.q.elikz =wqtype;

  }
  this.st.load(1,this.q)
}



  columns: SimpleTableColumn[] = [
    { title: '', index: 'key', type: 'checkbox' },


    {
      title: '操作',
      buttons: [
        {
          text: '发货详情',
          type: 'link',
          click: (record: any) => this.router.navigate(['/poList/poList/fh/'+record.ebeln+'/'+record.ebelp])
        },
        {
          text: '收货详情',
          type: 'link',
          click: (record: any) => this.router.navigate(['/poList/poList/sh/'+record.ebeln+'/'+record.ebelp])
        },
        {
          text: '批次详情',
          type: 'link',
          click: (record: any) => this.router.navigate(['/poList/poList/batchplan/'+record.ebeln+'/'+record.ebelp])
        }
      ]
    } ,

    // { title: '采购组织', index: 'ekorg' },
    // { title: '采购组', index: 'ejgrp' },
    { title: '是否已完成', index: 'elikz' ,render: 'elikz'},
    { title: '创建日期', index: 'bedat' },
    { title: '采购组', index: 'ejgrp' },

    { title: '供应商', index: 'lifnr' },
    { title: '供应商名称', index: 'name1' },
    { title: '采购订单', index: 'ebeln' },
    { title: '采购订单行项目', index: 'ebelp' },
    { title: '物料编码', index: 'matnr' },
    { title: '物料描述', index: 'maktx' },
    { title: '单位', index: 'meins' },
    { title: '供应商物料编码', index: 'idnlf' },
    { title: '供应商物料描述', index: 'lmaktx' },

    { title: '交货日期', index: 'eindt' },
    // { title: '净价', index: 'netpr' },
    // { title: '税码', index: 'nwskz' },
    { title: '退货标识', index: 'RETPO' },
    { title: '订单备注', index: 'ptext' },
    { title: '订单数', index: 'menge' },
    { title: '在途数', index: 'menge1' },
    { title: '冻结数', index: 'menge4' },
    { title: '已收货', index: 'menge2' },
    { title: '未清数', index: 'menge3' },
    { title: '客供数', index: 'menge5' },

    { title: '单据类型', index: 'zzph' },
    { title: '库存类型', index: 'zwxdkc' },
    { title: '是否备料', index: 'zsfbl' }
  ];

  constructor(
    private trans: TranslateService,
    public http: _HttpClient,
    public msg: NzMessageService,
    private modalSrv: ModalHelper,
    private comm: CommService,
    private cnt: Constant,
    private maService: maService,
    private emit: EmitterService,
    private router: Router,
    private modalService: NzModalService,
    private poListService: poListService,
    private notice: NzNotificationService,
    private spListService: spListService,

  ) {}
  syncing = false;
  lastDate = null;

  uploadProd(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
      console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.st.load();
      if(info.file.response[0].piccontent==='success'){
        this.notice.create('success', '成功', info.file.response[0].piccontent);
      }else{
        this.notice.create('error', '失败', info.file.response[0].piccontent);
      }
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '导入失败');
    }
  }
  getall(){
    if(!this.q.lifnr&&!this.q.ebeln){
      this.msg.warning("供应商编号或者采购订单号");
      return;
    }
    this.spListService.getekpo(this.q.lifnr,this.q.ebeln).pipe(
      catchError(err => { return of(err); })
    ).subscribe(ret=>{
      if (ret.ok) {
        this.msg.error("获取程序启动成功，请等待");
        this.st.reload();
      } else {
        this.msg.error(ret.message);
      }
    });


  }

  ngOnDestroy() {
    if (this.inter) {
      clearInterval(this.inter);
    }
  }
  inter
  ngOnInit() {
    this.imgSuffix = this.comm.getGoodsUrl();
    this.emit.getMessage().subscribe(data => {
      if (data.text === 'RELOAD_GOODS_LIST') {
        this.st.load(1, this.q);
      }
    })
  }
  fullChange(val: boolean) {
    // this.scroll = val ? { y: '350px' } : { y: '230px' };
  }

  checkboxChange(list: any[]) {
    this.checkedList = list;
  }




  packatta(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"ebeln",
        conValue:this.q.ebeln,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);

    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genpowq(this.q.ebeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

  packattaall(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"ebeln",
        conValue:this.q.ebeln,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);

    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genpoAll(this.q.ebeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

  opennewwindow(url){

    const newWindow = window.open(url, '_blank');
    newWindow.document.title = '下载';

  }

  close(): void {
    this.visible = false;
  }
  cancel() {
    this.repdate = null;
  }
  repdate = null;

  dateChange(result: Date[]): void {
    if (result.length !== 0) {
      this.q.startDate = formatDate(result[0], 'yyyy-MM-dd', 'en-US');
      this.q.endDate = formatDate(result[1], 'yyyy-MM-dd', 'en-US');
    } else {
      this.q.startDate = '';
      this.q.endDate = '';
    }
  }
}
